package org.zjkoye.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.micrometer.common.lang.Nullable;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zjkoye.crm.common.PageResultBean;
import org.zjkoye.crm.common.Result;
import org.zjkoye.crm.entity.User;
import org.zjkoye.crm.service.UserService;
import org.zjkoye.crm.utils.ThreadLocalUtil;

import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author zjkoye
 * @since 2024-06-20
 */
@Tag(name = "用户接口TEST")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    /**
     * 用户列表分页
     * @param pageSize 每页显示的条数
     * @param currentPage  要查询的页
     * @param name  用户姓名
     * @return  Result<PageResultBean<List<User>>>
     */
    @GetMapping("/pageList")
    @Operation(summary = "用户列表分页查询")
    @Parameters({
            @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token"),
            @Parameter(name = "pageSize", required = true, description = "每页显示的条数"),
            @Parameter(name = "currentPage", required = true, description = "要查询的页"),
            @Parameter(name = "name", description = "用户姓名", required = false)
    })
    public Result<PageResultBean<User>> pageList(@RequestParam int pageSize, @RequestParam int currentPage, @Nullable @RequestParam String name) {
        IPage<User> page = userService.pageList(pageSize, currentPage, name);
        if (page == null) {
            return Result.error("查询失败");
        }
        PageResultBean<User> pageResultBean = new PageResultBean<User>(page.getTotal(), page.getRecords());
        logger.info(String.valueOf(pageSize));
        logger.info(Result.success(PageResultBean.getInstance(page.getTotal(), page.getRecords())).toString());
        return Result.success(PageResultBean.getInstance(page.getTotal(), page.getRecords()));

    }



    /**
     * 获取当前登录用户信息
     * @return  User
     */
    @GetMapping("/currentUser")
    @Operation(summary = "获取当前登录用户信息")
    @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token")
    public Result<User> getCurrentUser() {
        Map<String, Object> userSession = ThreadLocalUtil.get();
        int id = (int) userSession.get("id");
        User user = userService.getUserById(id);
        if (user != null) {
            return Result.success(user);
        }
        return Result.error("用户状态异常");
    }

    /**
     * 添加用户
     * @param user
     * @return Result<String>
     */
    @PostMapping("/add")
    @Operation(summary = "添加用户")
    @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token")
    public Result<String> addUser(@RequestBody User user) {
        return userService.add(user);
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return Result<User>
     */
    @GetMapping("/getuserById")
    @Operation(summary = "根据id查询用户信息")
    @Parameters({
            @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token"),
            @Parameter(name = "id", required = true, description = "用户id")
    })
    public Result<User> getUserById(@RequestParam int id) {
        User user = userService.getUserById(id);
        if (user == null) {
            return Result.error("获取用户信息失败");
        }
        return Result.success(user);
    }

    /**
     * 修改用户信息
     * @param user
     * @return Result<String>
     */
    @PutMapping("/update")
    @Operation(summary = "修改用户信息")
    @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token")
    public Result<String> update(@RequestBody User user) {
        boolean result = userService.updateUser(user);
        if (!result) {
            return Result.error("修改失败");
        }
        return Result.success("修改成功");
    }

    /**
     * 删除用户信息
     * @param id
     * @return Result<String>
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除用户信息")
    @Parameters({
            @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token"),
            @Parameter(name = "id", required = true, description = "用户id")
    })
    public Result<String> delete(@PathVariable int id) {
        boolean result = userService.deleteById(id);
        if (!result) {
            return Result.error("删除失败");
        }
        return Result.success();
    }

}

